<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/8 0008
 * Time: 下午 5:04
 */

namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
use App\Model\User;
use Carbon\Carbon;
use Wxpay\Wechat;

class CommonController extends Controller
{
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if (!Session()->has('user_id')) {
                $redirect = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
                if (!$request->code) {
                    $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . env('WECHAT_APP_ID') . '&redirect_uri=' . $redirect . '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
                    header('Location: ' . $url);
                    exit;
                } else {
                    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . env('WECHAT_APP_ID') . '&secret=' . env('WECHAT_APP_SECRET') . '&code=' . $request->code . '&grant_type=authorization_code';
                    $res = json_decode(Wechat::httpGet($url), true);
                    $user = User::where('openid', $res['openid'])->first();
                    if (!$user) {
                        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $res['access_token'] . '&openid=' . $res['openid'] . '&lang=zh_CN';
                        $res = json_decode(Wechat::httpGet($url), true);
                        $dealer = 0; $extension = 0;
                        if(!empty($request->pid)){
                            $pinfo = User::find($request->pid);
                            //推广奖励操作
                            $this->extension($request->pid);
                            if($pinfo->type == 1){
                                $dealer = $pinfo->id;
                                $extension = 0;
                            }else{
                                $dealer = 0;
                                $extension = $pinfo->id;
                            }
                        }
                        $data = ['wc_nickname' => $res['nickname'], 'head' => $res['headimgurl'], 'openid' => $res['openid'], 'extension_id' => $extension, 'dealer_id' => $dealer, 'created_at' => time(), 'updated_at' => time()];
                        //保存用户
                        $id = User::insertGetId($data);
                        $u = ['id' => $id, 'wc_nickname' => $res['nickname'], 'head' => $res['headimgurl'], 'phone' => ''];
                        $this->_loginToken($u);
                        return;
                    }
                    $this->_loginToken($user->toArray());
                }
            } else {
                $user = User::where(session()->get('user_id'));
                define('UID', $user['id']);
                session()->put('nickname', $user['nickname']);
                session()->put('head_pic', $user['head_pic']);
                session()->put('phone', $user['phone']);
            }
            return $next($request);
        });
    }

    public function _loginToken($user)
    {
        define('UID', $user['id']);
        session()->put('nickname', $user['nickname']);
        session()->put('head_pic', $user['head_pic']);
        session()->put('phone', $user['phone']);
    }

    public function extension($id = 0)
    {
        User::where('id',$id)->increment('extension_num', 1);
        $user = User::find($id);
        switch ($user->extension_num){
            case '5':
                if($user->extension_type == 0){
                    User::update(['membership_time'=>Carbon::now()->addDays(5),'extension_type'=>1]);
                }
                break;
            case '15':
                if($user->extension_type == 1){
                    User::update(['membership_time'=>Carbon::now()->addDays(5),'extension_type'=>2]);
                }
                break;
            case '35':
                if($user->extension_type == 2){
                    User::update(['membership_time'=>Carbon::now()->addDays(10),'extension_type'=>3]);
                }
                break;
            case '65':
                if($user->extension_type == 3){
                    User::update(['membership_time'=>Carbon::now()->addDays(10),'extension_type'=>4]);
                }
                break;
            case '105':
                //推广奖励到此就获取完
                if($user->extension_type == 4){
                    User::update(['membership_time'=>Carbon::now()->addDays(20),'extension_num'=>0,'extension_type'=>5]);
                }
                break;
        }

    }
}